;( function () {
    "use strict";
   	var param_map = Result.getLocationParamObject();
    var category_id = param_map.category_id;
	 $( function () {
		 loadLeftCategory();
	 });
	 
	 
	 function loadLeftCategory(){
		 var dataList = Data_category;
		 var $template = $("#left_category_template"),
		 	 $list = $("#left_category_list");
		 $list.empty();
		 $list.append($("#first"));
		 if( $.isArray( dataList ) ) {
     		for( var i = 0, l = dataList.length; i < l; i++ ) {
     			var obj = $template.clone().removeAttr( "id" );
                	$list.append( Result.fillData( obj, dataList[i], "field" ));
            }
         }
		 if(category_id == null){
			 $("#left_category_list li.second").eq(0).removeClass("active").addClass("active");
			 var fir = $("#left_category_list li.second").eq(0).find('a').attr('category_id');
			 loadCategoryList(fir);
		 } else {
			 var ind = 0;
			 if(category_id != 4){
				 ind = category_id;
			 }
			 $("#left_category_list li.second").eq(ind).removeClass("active").addClass("active");
			 loadCategoryList(ind);
		 }
		
		 $("#left_category_list li.second").click(function(){
			 $("#left_category_list li.second").removeClass("active");
			 $(this).addClass("active");
			 loadCategoryList($(this).find('a').attr("category_id"));
		 });
	 }
	 
	 
	 function loadCategoryList(category){
		 var dataList = articleByCategory[category];
		 var $template1 = $("#one_pic_template"),
		 	 $template2 = $("#zero_pic_template"),
		 	 $template3 = $("#pic_template"),
		 	 $list = $("#news_list");
		 $list.empty();
		 if( $.isArray( dataList ) ) {
     		for( var i = 0, l = dataList.length; i < l; i++ ) {
     			var pl = dataList[i].images.length;
     			var obj;
     			if(pl == 0){
     				obj = $template2.clone().removeAttr( "id" );
     			} else if (pl > 0 && pl < 3){
     				obj = $template1.clone().removeAttr( "id" );
     			} else {
     				obj = $template3.clone().removeAttr( "id" );
     			}
     			$list.append( Result.fillData( obj, dataList[i], "field" ));
            }
         }
		 
		 $("#news_list").find(".to_detail").click(function(){
			 var newid = $(this).attr('new_id');
			 var category_id = $("#left_category_list li.second.active").find('a').attr("category_id");
			 Result.forward("newsDetail.html", {"new_id" : newid, "category_id" : category_id});
		 });
	 }
	
	 
	 
	 window.replace_left_category_name = function ( $field, name, rowVal ) {
		$field.html(name);
		$field.attr("category_id", rowVal['id']);
	 };
	 
	 window.replace_new_title = function ( $field, title, rowVal ) {
		$field.html(title);
		$field.attr("new_id", rowVal['id']);
		
	 };
	 
	 window.replace_images = function ( $field, id, rowVal ) {
		 var images = rowVal['images'];
		 var len = images.length;
		 
		 if(len < 3){
			 $field.attr("src", images[0]);
		 } else if (len >= 3){
			 $field.empty();
			 for(var i=0;i<len;i++){
				 var $h = '<div class=" am-padding-right-sm am-fl"><a class="font-color-1" href="javascript:void(0);">' +
				 '<img style="width:156px;height:100px;" class="am-img-responsive" src="'+images[i]+'"></a></div>';
				 $field.append($h);
			 }
			 
		 }
	 };
	 
		 
	
}( window ) );
